package com.itbaizhan.openhis.controller.erp;

import com.itbaizhan.openhis.domain.Producer;
import com.itbaizhan.openhis.dto.ProducerDto;
import com.itbaizhan.openhis.service.ProducerService;
import com.itbaizhan.openhis.utils.ShiroSecurityUtils;
import com.itbaizhan.openhis.vo.AjaxResult;
import com.itbaizhan.openhis.vo.DataGridView;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("erp/producter")
public class ProducerController {

    @Reference
    private ProducerService producerService;


    /**
     * 分页查询生产厂家
     * @param producerDto
     * @return
     */
    @GetMapping("listProducterForPage")
    public AjaxResult listProducterForPage(ProducerDto producerDto) {
        DataGridView dataGridView = producerService.listProducterForPage(producerDto);
        return AjaxResult.success("查询成功",dataGridView.getData(),dataGridView.getTotal());
    }


    @GetMapping("selectAllProducter")
    public AjaxResult selectAllProducter() {
        List<Producer> list = producerService.selectAllProducter();
        return AjaxResult.success(list);
    }


    /**
     * 添加生产厂家
     * @param producerDto
     * @return
     */
    @PostMapping("addProducter")
    public AjaxResult addProducter(@Validated ProducerDto producerDto) {
        producerDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(producerService.addProducter(producerDto));
    }

    /**
     * 修改生产厂家
     * @param producerDto
     * @return
     */
    @PutMapping("updateProducter")
    public AjaxResult updateProducter(@Validated ProducerDto producerDto) {
        producerDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(producerService.updateProducter(producerDto));
    }


    /**
     * 根据id查询厂家
     * @param producterId
     * @return
     */
    @GetMapping("getProducterById/{producterId}")
    public AjaxResult getProducterById(@PathVariable @Validated
                                       @NotNull(message = "查询厂家的ID不能为空") Long producterId) {
        return AjaxResult.success(producerService.getProducterById(producterId));
    }

    /**
     * 删除厂家
     * @param producterIds
     * @return
     */
    @DeleteMapping("deleteProducterByIds/{producterIds}")
    public AjaxResult deleteProducterByIds(@PathVariable @Validated
                                           @NotEmpty(message = "需要删除的厂家ID不能为空") Long[] producterIds) {
        return AjaxResult.toAjax(producerService.deleteProducterByIds(producterIds));
    }
}
